/**
 * Apr 3, 2010
 */
package org.tomale.id.ui.preference.pages;

import java.util.List;

import org.eclipse.core.runtime.preferences.ConfigurationScope;
import org.eclipse.core.runtime.preferences.IEclipsePreferences;
import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.jface.preference.PreferencePage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchPreferencePage;
import org.osgi.service.prefs.BackingStoreException;
import org.tomale.id.User;
import org.tomale.id.common.Helper;
import org.tomale.id.common.KeyValuePair;
import org.tomale.id.ui.Activator;

/**
 * @author ferd
 *
 */
public class UserPage extends PreferencePage implements
		IWorkbenchPreferencePage {

	Combo _cboProviders;
	
	List<KeyValuePair<String, String>> _providers;
	
	public UserPage(){
		setPreferenceStore(Activator.getDefault().getPreferenceStore());
	}
	
	/* (non-Javadoc)
	 * @see org.eclipse.jface.preference.PreferencePage#createContents(org.eclipse.swt.widgets.Composite)
	 */
	@Override
	protected Control createContents(Composite parent) {
		Composite comp = new Composite(parent, SWT.NULL);
		GridLayout layout = new GridLayout();
		layout.numColumns = 2;
		comp.setLayout(layout);
		
		Label l;
		
		l = new Label(comp, SWT.NONE);
		l.setText("User Provider");
		l.setLayoutData(new GridData());
		
		_cboProviders = new Combo(comp, SWT.DROP_DOWN | SWT.READ_ONLY);
		_cboProviders.setLayoutData(new GridData(SWT.FILL,SWT.BEGINNING,
				true,false));
		initProviders();
		
		setPreferences();
		
		return comp;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.ui.IWorkbenchPreferencePage#init(org.eclipse.ui.IWorkbench)
	 */
	@Override
	public void init(IWorkbench workbench) {

	}

	private void initProviders(){
		_providers = User.getProviders();
		for(KeyValuePair<String, String> kv : _providers){
			_cboProviders.add(kv.getValue());
		}
	}
	
	private void setPreferences(){
		IPreferenceStore store = getPreferenceStore();
		if(!store.getString(PreferenceConstants.USER_PID).isEmpty()){
			_cboProviders.setText(Helper.getPairByKey(_providers, 
					store.getString(PreferenceConstants.USER_PID)).getValue());
		}
	}
	
	private boolean valid(){
		return true;
	}
	
	private void save() throws BackingStoreException {
		if(valid()){
			IPreferenceStore store = getPreferenceStore();
			store.setValue(PreferenceConstants.USER_PID, 
					Helper.getPairByValue(_providers, _cboProviders.getText()).getKey());
			
			IEclipsePreferences pref = new ConfigurationScope().getNode(Activator.PLUGIN_ID);
			pref.flush();			
		}
	}

	@Override
	public boolean performOk() {
		try {
			save();
			return super.performOk();
		}catch(BackingStoreException e){
			setErrorMessage(e.getMessage());
			return false;
		}
			
	}

}
